yjx.controller('identityController', ['$rootScope', '$scope', '$fetch', 'modalService', '$locals', '$state', 'detailService', 'ipCookie', '$timeout', '$compile',
'$location',function ($rootScope, $scope, $fetch, modalService, $locals, $state, detailService, ipCookie, $timeout,$compile,$location) {
    var stateChangeApi = '/user/selectIdentity';
    $scope.state = 1;
    var wxState = $locals.get("wxState");
    //alert("wxSate  身份选择============="+wxState);
    //var wxState = "http://www.genetuan.com/#/surDetail?id=3&forwardId=2";
    //alert(wxState);
    //alert("userId=========="+$rootScope.userId);
    var absurl;
    if(wxState){
         absurl = wxState.split('://')[1]
    }
    //alert($location.absUrl())
    $scope.login = function () {
        console.log($scope.state);
        if( $rootScope.user.userType == 0 && $scope.state == 1 ) {
            //用户选择健康管家身份且没有申请成为健康管家 点击登录直接申请
            $timeout(function () {
                $scope.tempwx = "components/user/agency/agency.html";
                var html = '<div class="e-detail" ng-include="\'' + $scope.tempwx + '\'"></div>';
                var detailHtml = $compile(html)($scope);
                angular.element(document.querySelector('#m-detail')).append(detailHtml);//后排插入
                angular.element(document.querySelector('#m-detail')).show();
                $rootScope._dCount++;
            })
        }else{
            toLogin();
        }

    }

    function toLogin(){
        //此时用户是登录状态 告诉后端此时的用户是代理或普通
        $fetch.fetch(
            stateChangeApi,
            {
                params: {
                    userId: $rootScope.userId,
                    identity: $scope.state
                },
                cache: false,
            }
        ).then(function (data) {
                //接口返回200后将此时的用户状态保存到rootScope或cookie
                $rootScope.identity = data.identity;
                //var userData = ipCookie('user');
                //ipCookie('user', userData, ipCookiesParam);
                ipCookie('identity',data.identity,ipCookiesParam);
                detailService.killDetail(0);
                //页面跳转解析url
                //window.location.href = "http://www.genetuan.com/#/home"
                $timeout(function () {
                    if ((absurl && absurl.split('/#/') && absurl.split('/#/').length > 1) || (absurl && absurl.split('/') && absurl.split('/').length > 1)) {
                        encodeUrl();
                    } else {
                        $state.go("index");
                        //window.location.href = "http://www.genetuan.com/#/home"
                    }
                },1000)
            }).catch(function (e) {
                modalService.mainMsgError(e.msg);
            })
    }

    function cancel() {
        return
    }

    function encodeUrl() {
        if (absurl.split('/#/') && absurl.split('/#/').length > 1) {
            var url = absurl.split('/#/')[1];
        } else {
            url =absurl.split('/')[1];
            //console.log("url======================="+url);
        }
        if (url.split('?') && url.split('?').length > 1) {
            var index = url.split('?')[0];
            var params = url.split('?')[1];
            console.log(params)
            console.log(index);
            if (params) {
                //console.log(params);
                if (params.split('&') && params.split('&').length > 0) {
                    //暂时没有多个传参
                    params.split('&')[0]
                    var parames = {};
                    var paramsArr = params.split('&');
                    for (var i in paramsArr) {
                        var key = paramsArr[i].split('=')[0];
                        var value = paramsArr[i].split('=')[1];
                        parames[key] = value;
                    }
                    //console.log(parames)
                    $state.go(index, parames);
                }
                else {
                    var key = params.split('=')[0];
                    var value = params.split('=')[1];
                    var parames = {};
                    parames[key] = value;
                    $state.go(index, parames);
                }
            } else {
                $state.go(index);
            }

        } else {
           //if(url){
           //
           //}
            //window.location.href = "http://www.genetuan.com/#/home"
            $state.go("index");
        }
    }

    //console.log(wxState);
    //if(wxState && wxState.split('/#/') && wxState.split('/#/').length>1 ) {
    //    encodeUrl();
    //}
    $scope.changeState = function (id) {
        $scope.state = id;
    }
}])